package com.gadgets.busi.bm02.controller;

import com.alibaba.fastjson.JSON;
import com.gadgets.busi.bm02.common.GadgetsBm02Constant;
import com.gadgets.busi.bm02.common.GadgetsBm02ExceptionConstant;
import com.gadgets.busi.bm02.common.GadgetsBm02Utils;
import com.gadgets.busi.bm02.dto.*;
import com.gadgets.busi.bm02.pojo.MaterialDict;
import com.gadgets.busi.bm02.pojo.MaterialRecipe;
import com.gadgets.busi.bm02.service.IMaterialService;
import com.gadgets.busi.bm02.validator.McRecipeOperValidator;
import com.gadgets.framework.common.constant.GadgetsConstant;
import com.gadgets.framework.common.constant.GadgetsExceptionConstant;
import com.gadgets.framework.common.dto.HttpBaseResp;
import com.gadgets.framework.common.dto.Pagination;
import com.gadgets.framework.common.exception.GadgetsServiceException;
import com.gadgets.framework.common.util.ExcelExportUtil;
import com.gadgets.framework.common.util.NumberFormatUtil;
import com.gadgets.framework.common.util.StringUtils;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author UncleXiu
 * @version V1.0.0
 * @data 2020-11-02
 *
 */
@RestController
@RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/mcgame")
public class McMeterialControllerImpl {

    /**
     * Excel导出Sheet页
     */
    private final static List<String> EXCEL_EXPORT_SHEET_NAME = new ArrayList<>();
    /**
     * 组合Excel导出Sheet页
     */
    private final static List<String> MULTI_EXCEL_EXPORT_SHEET_NAME = new ArrayList<>();

    static {
        EXCEL_EXPORT_SHEET_NAME.add(GadgetsBm02Constant.MC_EXCEL_EXPORT_SHEET.RECIPE_TREE_SHEET);
        EXCEL_EXPORT_SHEET_NAME.add(GadgetsBm02Constant.MC_EXCEL_EXPORT_SHEET.MATERIAL_SHEET);
        MULTI_EXCEL_EXPORT_SHEET_NAME.add(GadgetsBm02Constant.MC_EXCEL_EXPORT_SHEET.MATERIAL_SHEET);
    }

    @Autowired
    @Qualifier(value = "mcMaterialServiceImpl")
    private IMaterialService materialService;
    @Autowired
    private McRecipeOperValidator recipeOperValidator;
    @Autowired
    private GadgetsBm02Utils gadgetsBm02Utils;
    @Value("${spring.downloadPath}")
    private String downLoadPath;
    /**
     * 最小权合成表信息
     */
    private ConcurrentHashMap<Long, List<MaterialRecipe>> map;

    private static final Logger logger = LoggerFactory.getLogger(McMeterialControllerImpl.class);

    /**
     * 新增配方表
     * @param request                       请求信息
     * @param response                      应答信息
     * @param recipeInfoDTO                 合成表信息
     * @throws GadgetsServiceException      异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/addRecipe.do")
    public void addRecipeInfo(HttpServletRequest request, HttpServletResponse response, RecipeInfoManageDTO recipeInfoDTO) throws GadgetsServiceException{
        // 执行校验
        recipeOperValidator.excute(recipeInfoDTO);
        // 数据对象转换
        RecipeInfoDTO excuteInfo = new RecipeInfoDTO();
        excuteInfo.setMeterialId(recipeInfoDTO.getMaterialId());
        List<RecipeItemDTO> itemList = new ArrayList<>();
        for (RecipeManageItemDTO itemDTO : recipeInfoDTO.getList()) {
            RecipeItemDTO info = new RecipeItemDTO();
            info.setMeterialId(itemDTO.getMaterialId());
            info.setDemandNum(itemDTO.getDemandNum());
            itemList.add(info);
        }
        excuteInfo.setList(itemList);
        // 保存数据
        materialService.addMeterialRecipe(excuteInfo);
    }

    /**
     * 更新配方表
     * @param request                       请求信息
     * @param response                      应答信息
     * @param recipeInfoDTO                 合成表信息
     * @throws GadgetsServiceException      异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/editRecipe.do")
    public void editRecipeInfo(HttpServletRequest request, HttpServletResponse response, RecipeInfoManageDTO recipeInfoDTO) throws GadgetsServiceException{
        // 执行校验
        recipeOperValidator.excute(recipeInfoDTO);
        RecipeInfoDTO excuteInfo = new RecipeInfoDTO();
        excuteInfo.setMeterialId(recipeInfoDTO.getMaterialId());
        List<RecipeItemDTO> itemList = new ArrayList<>();
        for (RecipeManageItemDTO itemDTO : recipeInfoDTO.getList()) {
            RecipeItemDTO info = new RecipeItemDTO();
            info.setMeterialId(itemDTO.getMaterialId());
            info.setDemandNum(itemDTO.getDemandNum());
            itemList.add(info);
        }
        excuteInfo.setList(itemList);
        // 保存数据
        materialService.editMeterialRecipe(excuteInfo);
    }

    /**
     * 查询物品列表
     * @param request                       请求信息
     * @param response                      应答信息
     * @param queryDTO                      查询条件
     * @param pagination                    分页信息
     * @return                              查询结果
     * @throws GadgetsServiceException      异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/dictQuery.do")
    public HttpBaseResp<MaterialDict> queryDictList(HttpServletRequest request, HttpServletResponse response, MaterialDictQueryDTO queryDTO, Pagination pagination) throws GadgetsServiceException{
        List<MaterialDict> resultList = materialService.selectMaterialDictByCondition(queryDTO, pagination);
        return new HttpBaseResp<>(resultList,pagination);
    }

    /**
     * 查询多个物品的联合合成表
     * @param request                       请求信息
     * @param response                      应答信息
     * @param queryDTO                      查询条件
     * @return                              查询结果
     * @throws GadgetsServiceException      异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/meterialCountMulti.do")
    public SumDemendsResultDTO multiCountQuery(HttpServletRequest request, HttpServletResponse response, String queryDTO) throws GadgetsServiceException{
        RecipeBatchQueryDTO recipeBatchQueryDTO = JSON.parseObject(queryDTO, RecipeBatchQueryDTO.class);
        List<RecipeQueryDTO> recipeQueryDTOList = recipeBatchQueryDTO.getRecipeQueryDtos();
        SumDemendsResultDTO result = new SumDemendsResultDTO();
        if(null!=recipeQueryDTOList && recipeQueryDTOList.size()>0){
            for (RecipeQueryDTO recipeQueryDTO : recipeQueryDTOList) {
                DemendsResultDTO resultDTO = meterialCountQuery(request,response,recipeQueryDTO);
                result.addOriMap(resultDTO);
            }
        }
        return result;
    }
    
    /**
     * 物品合成需求查询
     * @param request                       请求信息
     * @param response                      应答信息
     * @param recipeQueryDTO                查询条件
     * @return                              查询结果
     * @throws GadgetsServiceException      异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/meterialCount.do")
    public DemendsResultDTO meterialCountQuery(HttpServletRequest request, HttpServletResponse response, RecipeQueryDTO recipeQueryDTO) throws GadgetsServiceException{
        // 构建应答容器
        DemendsResultDTO resultDTO = new DemendsResultDTO();
        // 初次查询清单信息
        map = gadgetsBm02Utils.getListWeightRecipeCache();
        final MaterialDict materialDict = gadgetsBm02Utils.getMcMeterialDictItem(recipeQueryDTO.getMeterialId());
        if(GadgetsBm02Constant.ORI_FLAG_YES.equals(materialDict.getOriFlag())){
            throw new GadgetsServiceException(GadgetsBm02ExceptionConstant.RECIPE_IS_ORI, materialDict.getDictName());
        }
        final List<MaterialRecipe> recipeList = map.get(recipeQueryDTO.getMeterialId());
        // 如果缓存中找不到合成表全路径，就抛出异常
        if(null==recipeList || recipeList.size()==0){
            throw new GadgetsServiceException(GadgetsBm02ExceptionConstant.RECIPE_CACHE_NOT_FOUNT,new Object[]{});
        }
        RecipeResultDTO recipeResultDTO = new RecipeResultDTO();
        recipeResultDTO.setName(recipeQueryDTO.getMeterialName());
        recipeResultDTO.setNum(recipeQueryDTO.getProcessNum());
        resultDTO.setRecipeResultDTO(recipeResultDTO);
        for (final MaterialRecipe recipe : recipeList) {
            final MaterialDict dict = gadgetsBm02Utils.getMcMeterialDictItem(recipe.getMateriaId());
            // 将需求数量变更为 单个需求*总个数
            double multiNum = recipeQueryDTO.getProcessNum()/recipe.getGoodNum();
            if((int)multiNum-multiNum!=0.0D){
                Double rightProcessNum = NumberFormatUtil.roundUpForDouble(multiNum)*recipe.getGoodNum()+0.00;
                logger.debug("物品“{}”的需求数量应为{}的倍数，现在倍数为{}，不足倍数，总需求数需要调整为{}个",dict.getDictName(),recipe.getGoodNum(),multiNum,rightProcessNum);
                recipeQueryDTO.setProcessNum(rightProcessNum);
            }
            // 原料需要数量=需求数/生产单位
            Double demandNum = recipeQueryDTO.getProcessNum()/recipe.getGoodNum()*recipe.getDemandNum();
            // 如果当前物品不是原始物品，就需要进一步查询需求表
            if(GadgetsBm02Constant.ORI_FLAG_NO.equals(dict.getOriFlag())){
                // 组建查询结果表
                RecipeQueryDTO queryDTO = new RecipeQueryDTO();
                queryDTO.setMeterialId(recipe.getMateriaId());
                queryDTO.setMeterialName(gadgetsBm02Utils.getMcMeterialDictItem(recipe.getMateriaId()).getDictName());
                queryDTO.setProcessNum(demandNum);
                // 递归查询非原始物品的合成表，将子合成表递归到总表当中
                DemendsResultDTO demendsResultDTO = meterialCountQuery(request,response,queryDTO);
                HashMap<String,Integer> oriMap = demendsResultDTO.getOriMaterialMap();
                for (Map.Entry<String, Integer> recipeMap : oriMap.entrySet()) {
                    resultDTO.addOriMap(recipeMap.getKey(),recipeMap.getValue()+0.0);
                }
                // 如果是递归结构，需要将下级的合成表合并过来
                recipeResultDTO.addSonRecipe(demendsResultDTO.getRecipeResultDTO());
                recipeResultDTO.setName(materialDict.getDictName());
            }else {
                // 设置本级合成表
                RecipeResultDTO sonRecipeResultDTO = new RecipeResultDTO();
                sonRecipeResultDTO.setName(dict.getDictName());
                sonRecipeResultDTO.setNum(demandNum);
                recipeResultDTO.addSonRecipe(sonRecipeResultDTO);
                recipeResultDTO.setName(materialDict.getDictName());
                // 如果是原始材料，就直接加入容器
                resultDTO.addOriMap(dict.getDictName(), demandNum);
            }
        }
        return resultDTO;
    }

    /**
     * 物品合成需求查询
     * @param request                       请求信息
     * @param response                      应答信息
     * @param recipeQueryDTO                查询条件
     * @return                              查询结果
     * @throws GadgetsServiceException      异常信息
     */
    public List<MaterialRecipe> queryMeterialRecipe(HttpServletRequest request, HttpServletResponse response, RecipeQueryDTO recipeQueryDTO) throws GadgetsServiceException{
        return materialService.selectMaterialRecipeByCondition(recipeQueryDTO);
    }

    /**
     * 合成表Excel导出
     * @param request                       请求信息
     * @param response                      应答信息
     * @param recipeQueryDTO                合成查询信息
     * @return                              Excel文件流
     * @throws GadgetsServiceException      异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/meterialExcel.do")
    public HttpBaseResp excelExport(HttpServletRequest request, HttpServletResponse response, RecipeQueryDTO recipeQueryDTO) throws GadgetsServiceException, IOException {
        // 获取合成表
        DemendsResultDTO resultDTO = this.meterialCountQuery(request, response, recipeQueryDTO);
        String excelFileName = resultDTO.getRecipeResultDTO().getName()+resultDTO.getRecipeResultDTO().getNum() + UUID.randomUUID().toString();
        // 构建生成对象
        OutputStream excelOutputStream = ExcelExportUtil.getExcelOutputStream(excelFileName,downLoadPath);
        // 构建含Sheet页信息的Excel对象
        Workbook excelBook = ExcelExportUtil.getExcelBookObject(EXCEL_EXPORT_SHEET_NAME);
        List<CellStyle> style = buildExcelStyle(excelBook);
        // 构建合成表
        Sheet recipeSheet = excelBook.getSheet(GadgetsBm02Constant.MC_EXCEL_EXPORT_SHEET.RECIPE_TREE_SHEET);
        recipeSheet.setDefaultColumnWidth(7);
        buildRecipeSheet(recipeSheet,resultDTO.getRecipeResultDTO(),0,0, style);
        // 构建原料表
        Sheet materialSheet = excelBook.getSheet(GadgetsBm02Constant.MC_EXCEL_EXPORT_SHEET.MATERIAL_SHEET);
        buildMaterialSheet(resultDTO.getOriMaterialMap(), style, materialSheet);
        excelBook.write(excelOutputStream);
        return new HttpBaseResp();
    }

    /**
     * 合成表Excel导出
     * @param request                       请求信息
     * @param response                      应答信息
     * @param queryDTO                合成查询信息
     * @return                              Excel文件流
     * @throws GadgetsServiceException      异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/meterialExcelMulti.do")
    public HttpBaseResp excelExportMulti(HttpServletRequest request, HttpServletResponse response, String queryDTO) throws GadgetsServiceException, IOException {
        // 获取合成表
        SumDemendsResultDTO resultDTO = this.multiCountQuery(request, response, queryDTO);
        String excelFileName = UUID.randomUUID().toString();
        // 构建生成对象
        OutputStream excelOutputStream = ExcelExportUtil.getExcelOutputStream(excelFileName,downLoadPath);
        // 构建含Sheet页信息的Excel对象
        Workbook excelBook = ExcelExportUtil.getExcelBookObject(MULTI_EXCEL_EXPORT_SHEET_NAME);
        List<CellStyle> style = buildExcelStyle(excelBook);
        // 构建合成表
        for (RecipeResultDTO recipeResultDTO : resultDTO.getRecipeResultDTO()) {
            Sheet recipeSheet = excelBook.createSheet(recipeResultDTO.getName()+"（合成表）");;
            recipeSheet.setDefaultColumnWidth(7);
            buildRecipeSheet(recipeSheet,recipeResultDTO,0,0, style);
        }
        // 构建原料表
        Sheet materialSheet = excelBook.getSheet(GadgetsBm02Constant.MC_EXCEL_EXPORT_SHEET.MATERIAL_SHEET);
        buildMaterialSheet(resultDTO.getOriMaterialMap(), style, materialSheet);
        excelBook.write(excelOutputStream);
        return new HttpBaseResp();
    }

    /**
     * 构建原料表
     * @param resultDTO         合成表
     * @param style             样式
     * @param materialSheet     Sheet
     */
    protected void buildMaterialSheet(HashMap<String, Integer> resultDTO, List<CellStyle> style, Sheet materialSheet) {
        try {
            // 构建原料表格
            materialSheet.createRow(0);
            Cell materilTitleName = materialSheet.getRow(0).createCell(0);
            materilTitleName.setCellStyle(style.get(0));
            materilTitleName.setCellValue("物品名");
            Cell materilTitleNum = materialSheet.getRow(0).createCell(1);
            materilTitleNum.setCellStyle(style.get(1));
            materilTitleNum.setCellValue("数量");
            int rowNum = 1;
            // 循环遍历原料表
            for (Map.Entry<String, Integer> materialEntry : resultDTO.entrySet()) {
                materialSheet.createRow(rowNum);
                Cell name = materialSheet.getRow(rowNum).createCell(0);
                Cell num = materialSheet.getRow(rowNum).createCell(1);
                name.setCellStyle(style.get(0));
                name.setCellValue(materialEntry.getKey());
                num.setCellStyle(style.get(1));
                num.setCellValue(materialEntry.getValue());
                rowNum++;
            }
        } catch (Exception exception) {
            logger.error(StringUtils.formatExceptionStack(exception));
            throw new GadgetsServiceException(exception);
        }
    }

    /**
     * 构建Excel表格样式
     * @param excelBook 表格
     * @return          样式
     */
    protected List<CellStyle> buildExcelStyle(Workbook excelBook) {
        List<CellStyle> styleList = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            CellStyle style = excelBook.createCellStyle();
            Font font = excelBook.createFont();
            style.setBorderBottom(BorderStyle.THIN);
            style.setBorderLeft(BorderStyle.THIN);
            style.setBorderRight(BorderStyle.THIN);
            style.setBorderTop(BorderStyle.THIN);
            style.setAlignment(HorizontalAlignment.CENTER);
            style.setVerticalAlignment(VerticalAlignment.CENTER);
            style.setWrapText(true);
            style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            font.setFontHeightInPoints((short)9);
            style.setFont(font);
            if(i%2==0){
                style.setFillForegroundColor(HSSFColor.HSSFColorPredefined.LIGHT_YELLOW.getIndex());
                style.setFillBackgroundColor(HSSFColor.HSSFColorPredefined.LIGHT_YELLOW.getIndex());
            }else {
                style.setFillForegroundColor(HSSFColor.HSSFColorPredefined.LIGHT_GREEN.getIndex());
                style.setFillBackgroundColor(HSSFColor.HSSFColorPredefined.LIGHT_GREEN.getIndex());
            }
            styleList.add(style);
        }
        return styleList;
    }

    /**
     * 构建合成表Sheet页信息
     * @param recipeSheet                   合成表页对象
     * @param resultDTO                     合成表信息
     * @param level                         当前层级
     * @param cellStartNum                  横向起始位置
     * @param style                         单元格样式
     * @return                              横向结束位置
     * @throws GadgetsServiceException      异常信息
     */
    protected int buildRecipeSheet(Sheet recipeSheet,RecipeResultDTO resultDTO,final int level, int cellStartNum, List<CellStyle> style) throws GadgetsServiceException{
        try {
            int nameRow = level*2;
            int numRow = level*2+1;
            int recipeNum = cellStartNum;
            if(null==recipeSheet.getRow(nameRow)){
                recipeSheet.createRow(nameRow).setHeightInPoints(40f);
            }
            if(null==recipeSheet.getRow(numRow)){
                recipeSheet.createRow(numRow);
                recipeSheet.createRow(numRow).setHeightInPoints(40f);
            }
            List<RecipeResultDTO> sonRecipe = resultDTO.getSonList();
            if(sonRecipe.size()>0) {
                // 如果存在子合成表写入子合成表
                for (RecipeResultDTO recipeResultDTO : sonRecipe) {
                    // 写入子合成表并获取增量
                    recipeNum = buildRecipeSheet(recipeSheet, recipeResultDTO, level + 1, recipeNum, style);
                }
                // 合并合成表单元格并填写内容
                if(cellStartNum<recipeNum-1) {
                    addMergeCell(recipeSheet, resultDTO, cellStartNum, style, nameRow, numRow, recipeNum);
                }else {
                    insertCell(recipeSheet, resultDTO, cellStartNum, style, nameRow, numRow);
                }
                return recipeNum;
            }else {
                // 如果没有子合成表，直接在当前位置填写物品的合成信息
                insertCell(recipeSheet, resultDTO, cellStartNum, style, nameRow, numRow);
                return recipeNum+1;
            }
        } catch (Exception e) {
            logger.error(StringUtils.formatExceptionStack(e));
            throw new GadgetsServiceException(e);
        }
    }

    /**
     * 新增合并单元格内容
     * @param recipeSheet       Sheet对象
     * @param resultDTO         合成表信息
     * @param cellStartNum      物品表格起始列位置
     * @param style             单元格格式
     * @param nameRow           物品名称归属行
     * @param numRow            物品数量归属行
     * @param recipeNum         物品数量
     */
    private void addMergeCell(Sheet recipeSheet, RecipeResultDTO resultDTO, int cellStartNum, List<CellStyle> style, int nameRow, int numRow, int recipeNum) {
        recipeSheet.addMergedRegion(new CellRangeAddress(nameRow, nameRow, cellStartNum, recipeNum - 1));
        recipeSheet.addMergedRegion(new CellRangeAddress(numRow, numRow, cellStartNum, recipeNum - 1));
        Cell nameCell = recipeSheet.getRow(nameRow).createCell(cellStartNum);
        for (int i = cellStartNum; i < recipeNum; i++) {
            recipeSheet.getRow(nameRow).createCell(i).setCellStyle(style.get(0));
        }
        nameCell.setCellValue(resultDTO.getName());
        nameCell.setCellStyle(style.get(0));
        Cell numCell = recipeSheet.getRow(numRow).createCell(cellStartNum);
        for (int i = cellStartNum; i < recipeNum; i++) {
            recipeSheet.getRow(numRow).createCell(i).setCellStyle(style.get(1));
        }
        numCell.setCellValue(resultDTO.getNum());
        numCell.setCellStyle(style.get(1));
    }

    /**
     * 填充单元格
     * @param recipeSheet       表单页
     * @param resultDTO         合成信息
     * @param cellStartNum      单元格位置
     * @param style             样式
     * @param nameRow           名称行行数
     * @param numRow            数量行行数
     */
    protected void insertCell(Sheet recipeSheet, RecipeResultDTO resultDTO, int cellStartNum, List<CellStyle> style, int nameRow, int numRow) {
        Cell nameCell = recipeSheet.getRow(nameRow).createCell(cellStartNum);
        nameCell.setCellValue(resultDTO.getName());
        nameCell.setCellStyle(style.get(0));
        Cell numCell = recipeSheet.getRow(numRow).createCell(cellStartNum);
        numCell.setCellValue(resultDTO.getNum());
        numCell.setCellStyle(style.get(1));
    }

    /**
     * 新增物品信息
     * @param materialInfo                  物品信息
     * @return                              新增结果
     * @throws GadgetsServiceException      异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/addMaterial.do")
    public HttpBaseResp<Object> addMaterialInfo(String materialInfo) throws GadgetsServiceException{
        // 转换物品信息
        MaterialAddInfoDTO dto = JSON.parseObject(materialInfo, MaterialAddInfoDTO.class);
        // 保存物品及合成表信息
        materialService.addMaterialInfo(dto);
        return new HttpBaseResp<>();
    }

    /**
     * 数据导出
     * @return                              物品信息
     * @throws GadgetsServiceException      异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/dataExpot.do")
    public HttpBaseResp<Object> dataExport() throws GadgetsServiceException{
        materialService.exportMaterialAndRecipeInfo(downLoadPath);
        return new HttpBaseResp<>();
    }

    /**
     * 生产时间计算
     * @param calDTO                    生产信息
     * @return                          耗时
     * @throws GadgetsServiceException  异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/timeCalculate.do")
    public BigDecimal productTimeCalculate(TimeCalDTO calDTO) throws GadgetsServiceException{
        // 计算公式：耗时(s) = ( 单位成本 * 产量 ）/ ( 能量输入/t * 20 )
        double secondTm = (calDTO.getCost() * 1.0 * calDTO.getProductNum()) / (calDTO.getEnergyUnit() * 20.0);
        if(GadgetsConstant.TIME_UNIT_CONSTANT.SECOND.equals(calDTO.getTimeUnit())){
            // 输出秒
            return new BigDecimal(secondTm).setScale(0,BigDecimal.ROUND_HALF_UP);
        }else if(GadgetsConstant.TIME_UNIT_CONSTANT.MINITE.equals(calDTO.getTimeUnit())){
            // 输出分钟
            return new BigDecimal(secondTm / 60).setScale(3,BigDecimal.ROUND_HALF_UP);
        }else if(GadgetsConstant.TIME_UNIT_CONSTANT.HOUR.equals(calDTO.getTimeUnit())){
            // 输出小时
            return new BigDecimal(secondTm / (60 * 60)).setScale(3,BigDecimal.ROUND_HALF_UP);
        }else {
            // 单位不合法
            throw new GadgetsServiceException(GadgetsExceptionConstant.VALIDATE_ERROR,"时间单位不合法");
        }
    }

    /**
     * 全量更新物品拼音
     * @throws GadgetsServiceException  异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/pinyinGen.do")
    public void dictPinYinGenerator() throws GadgetsServiceException{
        materialService.pinyinGenerator();
    }


}
